import 'package:flutter/material.dart';
import 'package:flutter_one/class/appsetting/setting.dart';
import 'package:flutter_one/class/utils/Utils.dart';
import 'package:bmprogresshud/bmprogresshud.dart' as bmprogresshud;
import 'package:flutter_one/class/app/main/v/MainPageView.dart' as mpv;
import 'package:flutter_one/class/app/news/v/NewsPageView.dart' as npv;
import 'package:flutter_one/class/app/order/v/OrderPageView.dart' as opv;
import 'package:flutter_one/class/app/user/v/UserPageView.dart' as upv;
import 'package:provider/provider.dart';
import 'package:flutter_one/class/utils/ProviderSetup.dart';

/*底部TabBar*/
class BaseBottomNavigationBar extends StatefulWidget {
  const BaseBottomNavigationBar({super.key});

  @override
  State<BaseBottomNavigationBar> createState() =>
      _BaseBottomNavigationBarStatus();
}

class _BaseBottomNavigationBarStatus extends State<BaseBottomNavigationBar> {
  /*当前索引*/
  late int _currentIndex = 0;

  /*tabBar主页面*/
  late final List<Widget> _tabBarPageViews;
  late final PageView _pageView;

  late final PageController _pageController;

  /*tabBar页面集*/
  late final List<BottomNavigationBarItem> _pageBarItems = [
    BottomNavigationBarItem(
        icon: Image.asset('images/tabbar/home.png'),
        activeIcon: Image.asset('images/tabbar/home_hover.png'),
        label: '首页'),
    BottomNavigationBarItem(
        icon: Image.asset('images/tabbar/news.png'),
        activeIcon: Image.asset('images/tabbar/news_hover.png'),
        label: '资讯'),
    BottomNavigationBarItem(
        icon: Image.asset('images/tabbar/order.png'),
        activeIcon: Image.asset('images/tabbar/order_hover.png'),
        label: '订单'),
    BottomNavigationBarItem(
        icon: Image.asset('images/tabbar/mine.png'),
        activeIcon: Image.asset('images/tabbar/mine_hover.png'),
        label: '我的'),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _currentIndex = 0;

    _tabBarPageViews = [
      MultiProvider(providers: providers, child: const mpv.MainPageview()),
      MultiProvider(providers: providers, child: const npv.NewsPageView()),
      MultiProvider(providers: providers, child: const opv.OrderPageView()),
      MultiProvider(providers: providers, child: const upv.UserPageView())
    ];

    _pageController = PageController(
        // 初始索引值
        initialPage: _currentIndex,
        keepPage: true);

    _pageView = PageView(
      controller: _pageController,
      children: _tabBarPageViews,
      onPageChanged: (index) {
        setState(() {
          _currentIndex = index;
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        items: _pageBarItems,
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        backgroundColor: K_APP_BACKGROUND_COLOR,
        selectedItemColor: K_APP_TINT_COLOR,
        unselectedItemColor: Utils.shareInstance.hexToInt(0x505050),
        onTap: (int index) {
          if (index != _currentIndex && index < _tabBarPageViews.length) {
            // 控制 PageView 跳转到指定的页面
            _pageController.jumpToPage(index);

            setState(() {
              _currentIndex = index;
            });
          }
        },
      ),
      body: bmprogresshud.ProgressHud(
        maximumDismissDuration: K_APP_PROGRESS_HUD,
        isGlobalHud: true,
        child: _pageView,
      ),
    );
  }

  @override
  void dispose() {
    _pageController.dispose();

    // TODO: implement dispose
    super.dispose();

    debugPrint('$runtimeType 已销毁');
  }
}
